package sjtu.ist.warehouse4ws.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import sjtu.ist.warehouse4ws.dao.DepartmentDaoHibernate;
import sjtu.ist.warehouse4ws.dao.FileInfoDao;
import sjtu.ist.warehouse4ws.dao.OplogDao;
import sjtu.ist.warehouse4ws.dao.UserDao;
import sjtu.ist.warehouse4ws.model.Fileinfo;
import sjtu.ist.warehouse4ws.model.Oplog;
import sjtu.ist.warehouse4ws.model.User;
import sjtu.ist.warehouse4ws.tool.ListPage;
import sjtu.ist.warehouse4ws.vo.OplogVO;


public class OplogServiceImpl implements OplogService {
	private DepartmentDaoHibernate departmentDao;
	private OplogDao oplogDao;
	private UserDao userDao;
	private FileInfoDao fileInfoDao;

	public void setOplogDao(OplogDao oplogDao) {
		this.oplogDao = oplogDao;
	}
	
	public void setDepartmentDao(DepartmentDaoHibernate departmentDao) {
		this.departmentDao = departmentDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public void setFileInfoDao(FileInfoDao fileInfoDao) {
		this.fileInfoDao = fileInfoDao;
	}

	@Override
	public ListPage getOplogList(int start,int limit) {
		// TODO Auto-generated method stub
		return oplogDao.getAllOplogs(start,limit);
	}

	@Override
	public void saveOplog(Oplog oplog) {
		// TODO Auto-generated method stub
		this.oplogDao.save(oplog);
	}
	
	@Override
	public void updateOplog(Oplog oplog) {
		// TODO Auto-generated method stub
		oplogDao.update(oplog);
	}
	@Override
	public Oplog getOplog(String id) {
		// TODO Auto-generated method stub
		return oplogDao.get(id);
	}
	
	@Override
	public ListPage getOplogVOList(int start,int limit) {
		ListPage lp=oplogDao.getAllOplogs(start,limit);
		List<OplogVO> ovolist=new ArrayList<OplogVO>();
		List<Oplog> temp=lp.list;
		for(Oplog o : temp){
			OplogVO ovo=getOplogVO(o);
			ovolist.add(ovo);
		}
		lp.list=ovolist;
		return lp;
	}
	
	//获得vo
	private OplogVO getOplogVO(Oplog log){		
		String username="(ID)"+log.getUser();
		User user=userDao.get(log.getUser());
		if(user!=null){
			username=user.getName();
		}
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String ts=sdf.format(log.getTimestamp());
		
		String filename="(ID)"+log.getId();
		Fileinfo fi=fileInfoDao.get(log.getId());
		if(fi!=null){
			filename=fi.getName();
		}
		
		OplogVO ovo=new OplogVO(log.getId(),ts,username,log.getOperation(),filename,log.getDiscription());
		return ovo;
	}

	@Override
	public void deleteOplog(String id) {
		// TODO Auto-generated method stub
		oplogDao.delete(id);
	}

	@Override
	public ListPage searchOplog(String user, String after, String before,
			String file, String operation, String dspt,int start,int limit) {
		// TODO Auto-generated method stub
		
		ListPage lp=oplogDao.searchOplog(user, after, before, file, operation, dspt,start,limit);
		List<Oplog> temp=lp.list;
		List<OplogVO> ovolist=new ArrayList<OplogVO>();
		for(Oplog o : temp){
			OplogVO ovo=getOplogVO(o);
			ovolist.add(ovo);
		}
		lp.list=ovolist;
		return lp;
	}

	@Override
	public void recordLog(String user, String file, String dspt,
			String operation) {
		// TODO Auto-generated method stub
		String id=java.util.UUID.randomUUID().toString();
		Date timestamp=new Date();
		Oplog o=new Oplog(id,timestamp,user,operation,file,dspt);
		this.saveOplog(o);		
	}
}
